﻿@using Microsoft.JSInterop
@using Covenant.Models.Covenant
@using Covenant.Components.Shared
@inject IJSRuntime IJSRuntime

<CovenantTable TItem="CapturedTicketCredential" IsSearchable="IsSearchable" IsSortable="IsSortable" IsPaginated="IsPaginated" PageLength="PageLength" Items="TicketCredentials.ToList()" SearchRow="IsRowDisplayed" class="table table-hover">
    <TableHeader>
        <SortableTableHeader TItem="CapturedTicketCredential" TItem2="string" GetSortableProperty="(credential) => credential.Domain">
            <HeaderContent>Domain</HeaderContent>
        </SortableTableHeader>
        <SortableTableHeader TItem="CapturedTicketCredential" TItem2="string" GetSortableProperty="(credential) => credential.Username">
            <HeaderContent>Username</HeaderContent>
        </SortableTableHeader>
        <SortableTableHeader TItem="CapturedTicketCredential" TItem2="string" GetSortableProperty="(credential) => credential.ServiceName">
            <HeaderContent>ServiceName</HeaderContent>
        </SortableTableHeader>
        <SortableTableHeader TItem="CapturedTicketCredential" TItem2="TicketType" GetSortableProperty="(credential) => credential.TicketCredentialType">
            <HeaderContent>TicketCredentialType</HeaderContent>
        </SortableTableHeader>
        <SortableTableHeader TItem="CapturedTicketCredential" TItem2="string" GetSortableProperty="(credential) => credential.Ticket">
            <HeaderContent>Ticket</HeaderContent>
        </SortableTableHeader>
    </TableHeader>
    <TableRow Context="credential">
        <tr>
            <td>@credential.Domain</td>
            <td>@credential.Username</td>
            <td>@credential.ServiceName</td>
            <td>@credential.TicketCredentialType</td>
            <td>@credential.Ticket</td>
        </tr>
    </TableRow>
</CovenantTable>

@code {
    [Parameter]
    public IEnumerable<CapturedTicketCredential> TicketCredentials { get; set; }

    [Parameter]
    public bool IsSearchable { get; set; } = false;

    [Parameter]
    public bool IsSortable { get; set; } = true;

    [Parameter]
    public bool IsPaginated { get; set; } = true;

    [Parameter]
    public int PageLength { get; set; } = 10;

    private bool IsRowDisplayed(CapturedTicketCredential credential, string SearchTerm)
    {
        return SearchTerm == string.Empty ||
            credential.Domain.Contains(SearchTerm, StringComparison.CurrentCultureIgnoreCase) ||
            credential.Username.Contains(SearchTerm, StringComparison.CurrentCultureIgnoreCase) ||
            credential.ServiceName.Contains(SearchTerm, StringComparison.CurrentCultureIgnoreCase) ||
            credential.TicketCredentialType.ToString().Contains(SearchTerm, StringComparison.CurrentCultureIgnoreCase) ||
            credential.Ticket.Contains(SearchTerm, StringComparison.CurrentCultureIgnoreCase);
    }
}
